package com.qcby.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.admin.form.TagForm;
import com.qcby.admin.model.SgTag;
import com.qcby.admin.dao.SgTagMapper;
import com.qcby.admin.service.SgTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.admin.vo.PageVo;
import com.qcby.admin.vo.TagListVo;
import com.qcby.framwork.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 标签 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-07
 */
@Service
public class SgTagServiceImpl extends ServiceImpl<SgTagMapper, SgTag> implements SgTagService {
    @Autowired
    private SgTagMapper sgTagMapper;
    @Override
    public ResponseResult<PageVo> pageTagList(TagForm tagForm) {
        //分页查询
        Page page = new Page(tagForm.getPageNum(), tagForm.getPageSize());
        QueryWrapper<SgTag> queryWrapper = new QueryWrapper<>();
        //根据标签名进行查询
        queryWrapper.like(StringUtils.isNotBlank(tagForm.getName()), "name", tagForm.getName());
        //根据备注进行查询
        queryWrapper.like(StringUtils.isNotBlank(tagForm.getRemark()), "remark", tagForm.getRemark());
        //不显示已经删除的标签,0代表没有删除
        queryWrapper.eq("del_flag", 0);
        //进行分页
        Page<SgTag> result = sgTagMapper.selectPage(page, queryWrapper);
        List<TagListVo> tagListVos = new ArrayList<>();
        for (SgTag sgTag : result.getRecords()) {
            TagListVo tagListVo = new TagListVo();
            tagListVo.setId(sgTag.getId());
            tagListVo.setName(sgTag.getName());
            tagListVo.setRemark(sgTag.getRemark());
            tagListVos.add(tagListVo);
        }
        PageVo pageVo = new PageVo(tagListVos, tagListVos.size());
        //数据封装，返回
        return ResponseResult.okResult(pageVo);
    }
}
